package com.sikaryofficial.backend.controller.information;

import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sikaryofficial.backend.annotation.Repeat;
import com.sikaryofficial.backend.annotation.TenantFilter;
import com.sikaryofficial.backend.constant.BannerSectionPositionEnum;
import com.sikaryofficial.backend.domain.dto.SysDictDataDTO;
import com.sikaryofficial.backend.domain.dto.req.information.ArticleBannerH5ListReq;
import com.sikaryofficial.backend.domain.dto.req.information.ArticleBannerListReq;
import com.sikaryofficial.backend.domain.dto.req.information.ArticleBannerReq;
import com.sikaryofficial.backend.domain.dto.resp.information.ArticleBannerDTO;
import com.sikaryofficial.backend.manager.information.ArticleBannerManager;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.common.core.utils.HiResultUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.HiResult;
import com.sikaryofficial.common.log.annotation.Log;
import com.sikaryofficial.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 文章bannerController
 *
 * @author qinjinyuan
 * @date 2024-04-10
 */
@RestController
@RequestMapping("/article/banner")
@Api(tags = "资讯-文章banner")
public class ArticleBannerController extends BaseController {
    @Autowired
    private ArticleBannerManager articleBannerManager;

    @GetMapping("/{articleBannerId}/{visible}")
    @Log(title = "banner可见性设置", businessType = BusinessType.UPDATE)
    @ApiOperation("banner可见性设置")
    @TenantFilter
    public HiResult<Boolean> setVisible(@PathVariable("articleBannerId") Long articleBannerId,
                                        @PathVariable("visible") Integer visible) {
        return HiResultUtil.success(articleBannerManager.setVisible(articleBannerId, visible));
    }

    /**
     * 选择位置
     */
    @PostMapping("/getSectionPosition")
    @ApiOperation("选择位置")
    public R<List<SysDictDataDTO>> getSectionPosition() {
        BannerSectionPositionEnum[] reportTypeEnums = BannerSectionPositionEnum.values();
        List<SysDictDataDTO> result = new ArrayList<>();
        for (BannerSectionPositionEnum reportTypeEnum : reportTypeEnums) {
            SysDictDataDTO sysDictDataDTO = new SysDictDataDTO();
            result.add(sysDictDataDTO);
            sysDictDataDTO.setDictDesc(reportTypeEnum.getName());
            sysDictDataDTO.setDictValue(reportTypeEnum.getCode() + "");
            sysDictDataDTO.setDictType("section_position");
        }
        return R.ok(result);
    }
    /**
     * 查询文章banner列表
     */
    @PostMapping("/backend/listPage")
    @ApiOperation("banner后台管理列表")
    @TenantFilter
    public R<IPage<ArticleBannerDTO>> listPage(@Validated @RequestBody ArticleBannerListReq req) {
        return R.ok(articleBannerManager.listPage(req));
    }
    /**
     * 查询文章banner列表
     */
    @PostMapping("/list")
    @ApiOperation("H5-文章banner列表")
    @Repeat
    public R<List<ArticleBannerDTO>> list(@Validated @RequestBody ArticleBannerH5ListReq req) {
        return R.ok(articleBannerManager.list(req));
    }

    /**
     * 详情
     */
    @GetMapping("/backend/{articleBannerId}")
    @ApiOperation("banner后台管理-详情")
    @TenantFilter
    public R<ArticleBannerDTO> detail(@PathVariable(value = "articleBannerId") Long articleBannerId) {
        return R.ok(articleBannerManager.detail(articleBannerId));
    }

    /**
     * 新增或修改文章banner
     */
    @Log(title = "文章banner保存", businessType = BusinessType.INSERT)
    @PostMapping("/backend/save")
    @ApiOperation("banner后台管理-保存")
    @TenantFilter
    public R<Boolean> save(@Validated @RequestBody ArticleBannerReq req) {
        return R.ok(articleBannerManager.saveOrUpdate(req));
    }
    /**
     * 删除文章banner
     */
    // @RequiresPermissions("system:t:remove")
    @Log(title = "文章banner删除", businessType = BusinessType.DELETE)
    @GetMapping("/backend/delete/{articleBannerIds}")
    @ApiOperation("banner后台管理-删除")
    @TenantFilter
    public R<Boolean> remove(@ApiParam(value = "BannerIds,多个英文逗号分隔") @PathVariable String articleBannerIds) {
        if (CharSequenceUtil.isBlank(articleBannerIds)) {
            throw new ServiceException("articleBannerIds is blank");
        }
        return R.ok(articleBannerManager.remove(articleBannerIds));
    }
}
